<%@ page import="java.util.ArrayList" %>
<%@ page import="com.wy0924.bean.User" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/24/024
  Time: 16:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户列表</title>
    <script src="lib/js/jquery3.6.0.js"></script>
    <script src="lib/js/bootstrap.js"></script>
    <link rel="stylesheet" href="lib/css/bootstrap.css">
</head>
<body>
<button id="btn-logout">注销</button>
<table style="width: 500px;height: 300px;" class="table-bordered">

    <tr>
        <th>用户名</th>
        <th>IP</th>
        <th>登录时间</th>
        <th>操作</th>
    </tr>
    <%
        ServletContext servletContext = request.getServletContext();
        ArrayList<User> userList = (ArrayList<User>) servletContext.getAttribute("userList");
        if (userList != null) {
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
    %>

    <tr>
        <td><%=user.getUsername()%>
        </td>
        <td><%=user.getIP()%>
        </td>
        <td><%=user.getLoginTime()%>
        </td>
        <td>
            <button class="btn-remove" data-ip="<%=user.getIP()%>" >踢出</button>
        </td>
    </tr>
    <%
            }
        }

    %>
    <form action="">
        <input type="submit" value="提交">
    </form>
    <script type="text/javascript">
        $(function () {
            $("#btn-logout").click(function (event) {
                event.preventDefault();
                $.ajax({
                    url: "./login.do",
                    type: "GET",
                    contentType: "text",
                    data: {
                        action: "logout"
                    },
                    success: function (data) {
                        if (data === "0") {
                            window.location.href = "http://192.168.4.8:8080/d0924_homework/login.jsp"
                        }
                    }
                })
            });
            $(".btn-remove").click(function (event) {
                event.preventDefault();
                var ip = $(this).attr("data-ip");
                $.ajax({
                    url: "./login.do",
                    type: "GET",
                    contentType: "text",
                    data: {
                        action: "remove",
                        IP:ip
                    },
                    success: function (data) {
                        if (data === "-1") {
                            alert("权限不足 无法踢人")
                        }
                        window.location.href = "http://192.168.4.8:8080/d0924_homework/userList.jsp"
                    }
                })
            })
        })
    </script>
</table>
</body>
</html>
